//
//  ZCameraNavigationController.m
//  danpin
//
//  Created by chuangjia on 5/8/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//



@import AVFoundation;
#import "ZCameraNavigationController.h"
#import "ZCameraViewController.h"
#import "ZCameraPicker.h"
@interface ZCameraNavigationController ()

- (void)setupPickerWithDelegate:(id<ZCameraDelegate>)delegate;
- (void)setupAuthorizedWithDelegate:(id<ZCameraDelegate>)delegate;
- (void)setupDenied;
- (void)setupNotDeterminedWithDelegate:(id<ZCameraDelegate>)delegate;

@end



@implementation ZCameraNavigationController

+ (instancetype)newWithCameraDelegate:(id<ZCameraDelegate>)delegate
{
    ZCameraNavigationController *navigationController = [super new];
    navigationController.navigationBarHidden = YES;
    
    if (navigationController) {
        AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        
        switch (status) {
            case AVAuthorizationStatusAuthorized:
                [navigationController setupPickerWithDelegate:delegate];
                break;
                
            case AVAuthorizationStatusRestricted:
            case AVAuthorizationStatusDenied:
                [navigationController setupDenied];
                break;
                
            case AVAuthorizationStatusNotDetermined:
                [navigationController setupNotDeterminedWithDelegate:delegate];
                break;
        }
    }
    
    return navigationController;
}

#pragma mark -
#pragma mark - Private methods
- (void)setupPickerWithDelegate:(id<ZCameraDelegate>)delegate
{
    ZCameraPicker *picker =[ZCameraPicker new];
    picker.delegate =delegate;
    self.viewControllers = @[picker];
}
- (void)setupAuthorizedWithDelegate:(id<ZCameraDelegate>)delegate
{
    ZCameraViewController *viewController = [ZCameraViewController new];
    viewController.delegate = delegate;
    self.viewControllers = @[viewController];
}

- (void)setupDenied
{
//    UIViewController *viewController = [ZCameraAuthorizationViewController new];
//    self.viewControllers = @[viewController];
}

- (void)setupNotDeterminedWithDelegate:(id<ZCameraDelegate>)delegate
{
    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
        if (granted) {
            [self setupAuthorizedWithDelegate:delegate];
        } else {
            [self setupDenied];
        }
    }];
}

@end